#include "Bed.h"
#include "ui_Bed.h"

#include <QPainter>
#include <QPen>
#include <qmath.h>
#include <QDebug>
#include <QDateTime>
#include "c/pwm.h"
#include "CApi.h"
Bed::Bed(QWidget *parent) :
    MyDialog(parent),
    ui(new Ui::Bed)
{
    ui->setupUi(this);
    dialogName=MyDialog::BedDialog;
//    this->baseX=this->width()/2;
//    this->baseY=this->height()/2;
}

Bed::~Bed()
{
    delete ui;
}

void Bed::paintEvent(QPaintEvent *p)
{
    QPainter painter(this);
        //设置画笔
        QPen pen(QColor(0,0,255));
        //设置画笔宽度
        pen.setWidth(10);
        //设置画笔风格
        pen.setStyle(Qt::SolidLine);

        //让画家 使用这个笔
        painter.setPen(pen);

        //画线
        painter.drawLine(QPoint(baseX,baseY),QPoint(baseX+bedEndLength,baseY));//床尾
        painter.drawLine(QPoint(baseX+bedEndLength,baseY-bedEndHight/2),QPoint(baseX+bedEndLength,baseY+bedEndHight/2));//床尾梁
        painter.drawLine(QPoint(bedHead_DynamicX,bedHead_DynamicY),QPoint(baseX,baseY));//床头
        painter.drawLine(QPoint(bedHead_DynamicX,baseY+bedEndHight/2),QPoint(bedHead_DynamicX,baseY+bedEndHight/2-bedHeadHight));//床头梁
        painter.drawLine(QPoint(baseX-bedHeadLength-20,baseY+bedEndHight/2),QPoint(baseX+bedEndLength+20,baseY+bedEndHight/2));//床底
        painter.drawLine(QPoint(bedHead_DynamicX,bedHead_DynamicY-10),QPoint(pillowRightAngleX,pillowRightAngleY-10));//枕头
//        //画椭圆
//        painter.drawEllipse(QPoint(100,100),50,50);

//        //画矩形
//        QPen pen2(QColor(125,125,125));
//        painter.setPen(pen2);
//        painter.drawRect(QRect(50,50,50,50));

        //画文字
        // painter.drawText(QRect(10,200,150,150),"Hello,world");

        //画灯
        painter.drawLine(QPoint(baseX+lightToBedDistance-light_baseLength/2.0,baseY+bedEndHight/2),QPoint(baseX+lightToBedDistance+light_baseLength/2.0,baseY+bedEndHight/2));//灯底座
        painter.drawLine(QPoint(baseX+lightToBedDistance,baseY+bedEndHight/2),QPoint(baseX+lightToBedDistance,baseY+bedEndHight/2-lightMainPoleLength));//灯主杆
        painter.drawLine(QPoint(baseX+lightToBedDistance,baseY+bedEndHight/2-lightMainPoleLength),QPoint(baseX+lightToBedDistance-lightPole1,baseY+bedEndHight/2-lightMainPoleLength));//灯副杆1
        painter.drawLine(QPoint(baseX+lightToBedDistance-lightPole1,baseY+bedEndHight/2-lightMainPoleLength),QPoint(baseX+lightToBedDistance-lightPole1,baseY+bedEndHight/2-lightMainPoleLength+lightPole2));//灯副杆2

        QPointF points[4] = {
                   QPointF(baseX+lightToBedDistance-lightPole1-lightCoverUpLength/2.0, baseY+bedEndHight/2-lightMainPoleLength+lightPole2),
                   QPointF(baseX+lightToBedDistance-lightPole1+lightCoverUpLength/2.0, baseY+bedEndHight/2-lightMainPoleLength+lightPole2),
                   QPointF(baseX+lightToBedDistance-lightPole1+lightCoverDownLength/2.0, baseY+bedEndHight/2-lightMainPoleLength+lightPole2+lightCoverHight),
                   QPointF(baseX+lightToBedDistance-lightPole1-lightCoverDownLength/2.0, baseY+bedEndHight/2-lightMainPoleLength+lightPole2+lightCoverHight)
               };
       painter.drawPolygon(points, 4);// 绘制梯形罩

       QPointF pointLigth[4] = {
                  QPointF(baseX+lightToBedDistance-lightPole1-lightWidget/2.0, baseY+bedEndHight/2-lightMainPoleLength+lightPole2+lightCoverHight),
                  QPointF(baseX+lightToBedDistance-lightPole1+lightWidget/2.0, baseY+bedEndHight/2-lightMainPoleLength+lightPole2+lightCoverHight),
                  QPointF(baseX+lightToBedDistance-lightPole1+lightWidget/2.0, baseY+bedEndHight/2-lightMainPoleLength+lightPole2+lightCoverHight+lightHight),
                  QPointF(baseX+lightToBedDistance-lightPole1-lightWidget/2.0, baseY+bedEndHight/2-lightMainPoleLength+lightPole2+lightCoverHight+lightHight)
              };
       //painter.setBrush(Qt::blue);
       painter.drawPolygon(pointLigth, 4);// 绘制灯

       switch (lightStatus) {
       case close:
           //默认
           break;
       case half:
       {
           qDebug()<<"half"<<endl;
           QPointF pointLigth[4] = {
                      QPointF(baseX+lightToBedDistance-lightPole1-lightWidget/2.0, baseY+bedEndHight/2-lightMainPoleLength+lightPole2+lightCoverHight),
                      QPointF(baseX+lightToBedDistance-lightPole1+lightWidget/2.0, baseY+bedEndHight/2-lightMainPoleLength+lightPole2+lightCoverHight),
                      QPointF(baseX+lightToBedDistance-lightPole1+lightWidget/2.0, baseY+bedEndHight/2-lightMainPoleLength+lightPole2+lightCoverHight+lightHight/2),
                      QPointF(baseX+lightToBedDistance-lightPole1-lightWidget/2.0, baseY+bedEndHight/2-lightMainPoleLength+lightPole2+lightCoverHight+lightHight/2)
                  };
           painter.setBrush(QColor(0,0,255));
           painter.drawPolygon(pointLigth, 4);

       }
           break;
       case entire:
       {qDebug()<<"entire"<<endl;
           QPointF pointLigth[4] = {
                      QPointF(baseX+lightToBedDistance-lightPole1-lightWidget/2.0, baseY+bedEndHight/2-lightMainPoleLength+lightPole2+lightCoverHight),
                      QPointF(baseX+lightToBedDistance-lightPole1+lightWidget/2.0, baseY+bedEndHight/2-lightMainPoleLength+lightPole2+lightCoverHight),
                      QPointF(baseX+lightToBedDistance-lightPole1+lightWidget/2.0, baseY+bedEndHight/2-lightMainPoleLength+lightPole2+lightCoverHight+lightHight),
                      QPointF(baseX+lightToBedDistance-lightPole1-lightWidget/2.0, baseY+bedEndHight/2-lightMainPoleLength+lightPole2+lightCoverHight+lightHight)
                  };
           painter.setBrush(QColor(0,0,255));
           painter.drawPolygon(pointLigth, 4);

       }
           break;

       }
}

void Bed::setBedAngle(double angle)//参数为角度
{qDebug()<<"angle："<<angle<<endl;
    double radians=qDegreesToRadians(angle);
    double sinValueY = qSin(radians);
    double lengthY=sinValueY*bedHeadLength;
    this->bedHead_DynamicY=baseY-lengthY;
    double cosValueX = qCos(radians);
    double lengX=cosValueX*bedHeadLength;
    this->bedHead_DynamicX=baseX-lengX;
    qDebug()<<"bedHead_DynamicX:"<<bedHead_DynamicX<< "   bedHead_DynamicY:"<<bedHead_DynamicY<<endl;

    this->pillowRightAngleY=sinValueY*this->pillowLength+this->bedHead_DynamicY;
    this->pillowRightAngleX=cosValueX*this->pillowLength+this->bedHead_DynamicX;

    this->bedHeadHight=lengthY+this->bedEndHight/2+HeadHightMoreEnd;
    this->update();
}

void Bed::mousePressEvent(QMouseEvent *event)
{
    if(event->pos().x()>=bedHead_DynamicX &&
            event->pos().x()<=baseX &&
            event->pos().y()>=(bedHead_DynamicY-30) &&
            event->pos().y()<=(baseY+30))
    {
        this->bedIsActive?bedIsActive=false:bedIsActive=true;
        if(this->bedIsActive)
        {qDebug()<<"床控制"<<endl;
            qDebug()<<"this->bedControl1:"<<this->bedControl;
            showControl(this->bedControl,"bed");
            qDebug()<<"this->bedControl2:"<<this->bedControl;
        }
        else
        {qDebug()<<"bedIsActive:"<<bedIsActive<<endl;
            hideControl(this->bedControl);
        }

    }
    else if(event->pos().x()>=baseX+lightToBedDistance-lightPole1-lightCoverDownLength/2.0 &&
            event->pos().x()<=baseX+lightToBedDistance-lightPole1+lightCoverDownLength/2.0 &&
            event->pos().y()>=baseY+bedEndHight/2-lightMainPoleLength+lightPole2 &&
            event->pos().y()<=baseY+bedEndHight/2-lightMainPoleLength+lightPole2+lightCoverHight)
    {
        this->lightIsActive?lightIsActive=false:lightIsActive=true;
        if(this->lightIsActive)
        {qDebug()<<"灯控制"<<endl;
            showControl(this->lightControl,"light");
        }
        else
            hideControl(this->lightControl);
    }
    else
    {
        if(this->bedControl!=nullptr)
        {
            this->bedControl->hide();
            bedIsActive=false;
        }
        if(this->lightControl!=nullptr)
        {
            this->lightControl->hide();
            lightIsActive=false;
        }
    }
}

void Bed::showControl(cSliderDateTime *&control,QString controlName)
{qDebug()<<"control:"<<control<<endl;
    if(control==nullptr)
    {
            control=new cSliderDateTime(controlName,this);
            qDebug()<<"control:"<<control<<endl;
            control->setWindowFlag(Qt::FramelessWindowHint);
            control->setStyleSheet("background-color:white;");
            if(controlName=="bed")
            {
                control->setGeometry(this->baseX-this->bedHeadLength-this->controlWight-30,this->baseY-this->controlHight/2,this->controlWight,this->controlHight);
                control->createBedSlider();
                connect(control->sliderYear,&cSlider::currentValueChanged,this,&Bed::bedCurrentValueChanged);

            }
            else if(controlName=="light")
            {
                control->setGeometry(baseX+lightToBedDistance+30,baseY+bedEndHight/2-lightMainPoleLength,this->controlWight,this->controlHight);
                    control->createLightSlider();
                    connect(control->sliderYear,&cSlider::currentValueChanged,this,&Bed::lightCurrentValueChanged);
            }

    }
    control->show();
}

void Bed::hideControl(cSliderDateTime *&control)
{qDebug()<<"hideControl"<<endl;
    control->hide();
}

void Bed::bedCurrentValueChanged(QString currentValue)
{
    qDebug()<<"床值改变了"<<currentValue<<endl;
    double ag;
    if(currentValue=="1档")
    {
        ag=0;
        tuo_control(functionFlag::bed_1);
    }
    else if(currentValue=="2档")
    {
        ag=20;
        tuo_control(functionFlag::bed_2);
    }
    else if(currentValue=="3档")
    {
        ag=40;
        tuo_control(functionFlag::bed_3);
    }
    else if(currentValue=="4档")
    {
        ag=60;
        tuo_control(functionFlag::bed_4);
    }
    this->setBedAngle(ag);
}

void Bed::lightCurrentValueChanged(QString currentValue)
{
    qDebug()<<"灯值改变了"<<currentValue<<endl;
    if(currentValue.indexOf("0")>=0)
    {
        this->lightStatus=close;
        led_control(functionFlag::light_close);
    }
    else if(currentValue.indexOf("1")>=0)
    {
        this->lightStatus=half;
        led_control(functionFlag::light_openhalf);
    }
    else if(currentValue.indexOf("2")>=0)
    {
        this->lightStatus=entire;
        led_control(functionFlag::light_openall);
    }
    qDebug()<<"灯值改变了"<<this->lightStatus<<endl;
    this->update();
}

void Bed::siriWork(MyDialogName name,functionFlag flag)
{qDebug()<<"bed siriWork"<<endl;
    switch (flag) {
    case light_close:
        lightCurrentValueChanged("0档");
        break;
    case light_openhalf:
        lightCurrentValueChanged("1档");
        break;
    case light_openall:
        lightCurrentValueChanged("2档");
        break;
    case bed_1:
        bedCurrentValueChanged("1档");
        break;
    case bed_2:
        bedCurrentValueChanged("2档");
        break;
    case bed_3:
        bedCurrentValueChanged("3档");
        break;
    case bed_4:
        bedCurrentValueChanged("4档");
        break;
    case functionFlag::call_yes:
        emit this->showPatient_signal(MyDialog::showPatientInfor);
        break;
    case functionFlag::call_cancel:
        if(name!=this->dialogName)
            this->showPatient_signal("");
        else if(name==this->dialogName)
        {
            this->showPatient_signal("");
            emit this->siriWork_signal(name,flag);
        }
        break;
    default:
        emit this->siriWork_signal(name,flag);
    }
}


void Bed::on_over_clicked()
{
    this->hide();
}

